﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;
using edowork.com.Model;

namespace WebInvoicingsys.system.baseset.basedata
{
    public partial class job_edit : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        string id = "", jobremark = "";

        private void bind()
        {
            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "", error_msg = "";

            sql = @"select * from job where jobid = '{0}'";

            sql = String.Format(sql, id);

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if ((ds == null) || (ds.Tables.Count <= 0) || (ds.Tables[0].Rows.Count <= 0))
            {
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                txt_jobremark.Text = dr["jobremark"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    id = Request.Params["id"].Trim();
                    jobremark = Request.Params["jobremark"].Trim();
                }
                catch
                {
                    Response.Redirect("/system/baseset/basedata/job.aspx");
                    return;
                }

                bind();
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                id = Request.Params["id"].Trim();
            }
            catch
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, Errormsg.er_Parampost);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return;
            }

            alert_msg = "";

            edowork.com.Model.jobInfo jobInfo = new jobInfo();

            jobInfo.Jobremark = txt_jobremark.Text.Trim();

            if (String.IsNullOrEmpty(jobInfo.Jobremark))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入职务描述");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_jobremark.Focus();

                return;
            }

            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //保存入department表
                sql = String.Format("update job set jobremark='{1}' where jobid='{0}'", id, jobInfo.Jobremark);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Save);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            try
            {
                id = Request.Params["id"].Trim();
                jobremark = Request.Params["jobremark"].Trim();
            }
            catch
            {
                Response.Redirect("/system/baseset/basedata/department.aspx");
                return;
            }

            string url = String.Format("/system/baseset/basedata/job_list.aspx?jobremark={0}", jobremark);

            Response.Redirect(url);
        }
    }
}